//
//  FoundDetailsViewController.m
//  TodatPlay
//
//  Created by dllo on 16/9/10.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "FoundDetailsViewController.h"
#import "DetailsCollectionViewCell.h"
#import "AskingCollectionViewCell.h"
#import "SceneCollectionViewCell.h"
#import "TAMacro.h"
#import "SDAutoLayout.h"
#import "UIView+Frame.h"
#import "CDMNetTool.h"
// 详情model
#import "DetailsRootClass.h"
#import "DetailsProjinfo.h"
#import "DetailsData.h"

#import "FoundDetailsRootClass.h"
#import "FoundDetailsData.h"
#import "FoundDetailsProjinfo.h"
#import "FoundDetailsF.h"
#import "FoundDetailsDamaiprojinfo.h"
#import "FoundDetailsL.h"
#import "FoundDetailsP.h"

#import "FoundDetailsCollectionViewCell.h"




// 宏
#define kCollectionViewCellDetailsReuse @"detailsReuse"
#define kCollectionViewCellAskingReuse @"askingReuse"
#define kCollectionViewCellSceneReuse @"sceneReuse"
@interface FoundDetailsViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UICollectionView *detailsCollectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *detailsLayout;
@property (nonatomic, strong) UIView *lineView;
//@property (nonatomic, strong) DetailsRootClass *detailsRootData;
@property (nonatomic, strong) FoundDetailsRootClass *detailsRootData;
@end

@implementation FoundDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self configCollectionView];
    [self confignavigation];
    [self handleDetailsData];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"left_icon"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]  style:UIBarButtonItemStylePlain target:self action:@selector(handleLeft:)];
    
}
#pragma mark - 实现左按钮的点击方法
- (void)handleLeft:(UIBarButtonItem *)barBtn {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setParm:(NSString *)parm {
    _parm = parm;
    [self.detailsCollectionView reloadData];
}
- (void)handleDetailsData {
    NSString *str = @"http://wanapi.damai.cn/proj.json?";
    NSString *idStr = [NSString stringWithFormat:@"id=%@", _parm];
    NSString *versionStr = @"version=203004";
    NSString *finalStr = [[str stringByAppendingFormat:@"%@", idStr] stringByAppendingFormat:@"&%@", versionStr];
    
    [CDMNetTool GET:finalStr andBody:nil andHeader:nil andResponse:CDMJSON andSuccessBlock:^(id result) {
//                NSLog(@"请求成功:%@", result);
        self.detailsRootData = [FoundDetailsRootClass modelWithDic:result];
//        NSLog(@"%@", self.detailsRootData.data);
        [self.detailsCollectionView reloadData];
    } andFailureBlock:^(NSError *error) {
        NSLog(@"请求失败%@", error);
    }];
    
}
- (void)confignavigation {
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"left_icon"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ] style:UIBarButtonItemStylePlain target:self action:@selector(clickLeftBarButton:)];
    UILabel *detailedLabel = [UILabel new];
    UILabel *askingLabel = [UILabel new];
    UILabel *sceneLabel = [UILabel new];
    UIView *backView = [UIView new];
    backView.backgroundColor = [UIColor clearColor];
    self.lineView = [UIView new];
    _lineView.backgroundColor = [UIColor whiteColor];
    detailedLabel.text = @"详情";
    askingLabel.text = @"提问";
    sceneLabel.text = @"现场";
    detailedLabel.textAlignment = NSTextAlignmentCenter;
    askingLabel.textAlignment = NSTextAlignmentCenter;
    sceneLabel.textAlignment = NSTextAlignmentCenter;
    detailedLabel.textColor = [UIColor whiteColor];
    askingLabel.textColor = [UIColor whiteColor];
    sceneLabel.textColor = [UIColor whiteColor];
    [backView addSubview:detailedLabel];
    [backView addSubview:askingLabel];
    [backView addSubview:sceneLabel];
    [backView addSubview:askingLabel];
    [backView addSubview:_lineView];
    backView.frame = CGRectMake(0, 0, 180, 44);
    askingLabel.sd_layout
    .centerYEqualToView(backView)
    .centerXEqualToView(backView)
    .widthIs(60)
    .heightIs(30);
    detailedLabel.sd_layout
    .leftSpaceToView(backView, 0)
    .centerYEqualToView(backView)
    .widthIs(60)
    .heightIs(30);
    sceneLabel.sd_layout
    .rightEqualToView(backView)
    .centerYEqualToView(backView)
    .widthIs(60)
    .heightIs(30);
    
    _lineView.frame = CGRectMake(0, 0, 50, 2);
    _lineView.center = CGPointMake(30, 40);
    
    self.navigationItem.titleView = backView;
    UITapGestureRecognizer *detailsedTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(detailedTap:)];
    UITapGestureRecognizer *askingTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(askingTap:)];
    UITapGestureRecognizer *sceneTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sceneTap:)];
    [detailedLabel addGestureRecognizer:detailsedTap];
    [askingLabel addGestureRecognizer:askingTap];
    [sceneLabel addGestureRecognizer:sceneTap];
    detailedLabel.userInteractionEnabled = YES;
    askingLabel.userInteractionEnabled = YES;
    sceneLabel.userInteractionEnabled = YES;
    
}
#pragma mark - 左button的点击方法
- (void)clickLeftBarButton:(UIBarButtonItem *)barButton {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)configCollectionView {
    [self.detailsCollectionView registerNib:[UINib nibWithNibName:@"SceneCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:kCollectionViewCellSceneReuse];
    
    [self.detailsCollectionView registerNib:[UINib nibWithNibName:@"AskingCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:kCollectionViewCellAskingReuse];
    [self.detailsCollectionView registerClass:[FoundDetailsCollectionViewCell class] forCellWithReuseIdentifier:kCollectionViewCellDetailsReuse];
    self.detailsLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.detailsLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.detailsLayout.minimumLineSpacing = 0;
    //    self.detailsLayout.minimumInteritemSpacing = 0;
    self.detailsCollectionView.bounces = NO;
    self.detailsCollectionView.pagingEnabled = YES;
    self.detailsCollectionView.showsHorizontalScrollIndicator = NO;
}
#pragma mark - Datasource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 3;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
       FoundDetailsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewCellDetailsReuse forIndexPath:indexPath];
        cell.data = self.detailsRootData.data;
        cell.projinfo = self.detailsRootData.data.projinfo;
        return cell;
    } else if (indexPath.row == 1) {
        AskingCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewCellAskingReuse forIndexPath:indexPath];
        
        return cell;
    } else {
        SceneCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewCellSceneReuse forIndexPath:indexPath];
        
        return cell;
    }
}
#pragma mark - 返回item大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - 64);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - 滑动条
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (scrollView.contentOffset.x / SCREEN_WIDTH < 0.1) {
        [UIView animateWithDuration:0.3 animations:^{
            self.lineView.dm_centerX = 30;
        }];
    } else if (scrollView.contentOffset.x / SCREEN_WIDTH >= 0.9 && scrollView.contentOffset.x / SCREEN_WIDTH < 1.10) {
        [UIView animateWithDuration:0.3 animations:^{
            self.lineView.dm_centerX = 90;
        }];
    } else if (scrollView.contentOffset.x / SCREEN_WIDTH >= 1.9){
        [UIView animateWithDuration:0.3 animations:^{
            self.lineView.dm_centerX = 150;
        }];
    }
    
}
#pragma mark - 手势
- (void)sceneTap:(UITapGestureRecognizer *)tap {
    [UIView animateWithDuration:0.4 animations:^{
        self.detailsCollectionView.contentOffset = CGPointMake(SCREEN_WIDTH * 2-0.5 , -64);
    }];
}
- (void)detailedTap:(UITapGestureRecognizer *)tap {
    [UIView animateWithDuration:0.4 animations:^{
        self.detailsCollectionView.contentOffset = CGPointMake(0.5, -64);
    }];
}
- (void)askingTap:(UITapGestureRecognizer *)tap {
    [UIView animateWithDuration:0.4 animations:^{
        self.detailsCollectionView.contentOffset = CGPointMake(SCREEN_WIDTH -0.5, -64);
    }];
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
